﻿using System.Web.Mvc;
using System.Web.Routing;
using CRM.Models;

namespace CRM.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class CheckAccessAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool isDenied = PermissionsManager.ValidatePermissions(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                                                   filterContext.ActionDescriptor.ActionName,
                                                   filterContext.RequestContext.HttpContext.User.Identity.Name);
            if(!isDenied)
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary { { "controller", "Home" }, { "action", "Error" }, { "name", "NotExists" } });
            }
        }
    }
}